<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class XcUserConsumerDetails extends Model
{
    protected $table = 'xc_user_consumer_details';
    protected $guarded = ['id'];

    public function user()
    {
        return $this->belongsTo('App\Models\XcUser', 'id', 'user_id');
    }

    public function userRecharge()
    {
        return $this->hasOne('App\Models\XcUserRecharge', 'order_sn', 'order_no');
    }

    /**
     * 钱包及积分金额变动日志
     * @param $user_id
     * @param $num
     * @param $type (变动类型) 1、订单充值赠送 2、订单消耗 3、企业卡券赠送 4、过期 5、手动积分调整 6、订单后台退款
     * @param $order_no
     * @param $change_type 1、金额 2、积分 3、次卡变动
     * @param $site_name 1 站点名称
     * @param int $relation_id
     * @param int $washing_type 洗车类型 1、快洗 2、精洗
     * @param int $operator 操作者
     * @internal param $member_id
     */
    public function changeLog($user_id, $num, $type, $order_no, $change_type, $site_name, $relation_id = 0, $washing_type = 0, $operator = 0)
    {
        if ($type == 2 || $type == 4 || $type == 6) {
            $num = (-1) * $num;
        }

        if ($num != 0) {
            $this->create([
                'user_id' => $user_id,
                'change_type' => $change_type,
                'site_name' => $site_name,
                'num' => $num,
                'type' => $type,
                'order_no' => $order_no,
                'relation_id' => $relation_id,
                'washing_type' => $washing_type,
                'operator' => $operator
            ]);
        }
    }
}
